function FormatNumberEntity (numberFormat, fullFormatOpt) {
    this.fullFormat = false;

    this.decimalSeparate = " ";  
    this.fractionSeparate = "."; 
    this.decimalGroupCount = 3; 
    this.fractionCount = 0;   
    
    this.numberFormat = "";
    this.validNumberFormat = false;
    this.init = initEntity;

    if (numberFormat) {
        this.numberFormat = numberFormat;
    } // if
    try {
        if (fullFormatOpt == true) {
            this.fullFormat = true;
        } else {
            this.fullFormat = false;
        }
    } catch (ex) {}

    this.isValidNumber = isValidNumber;
    this.getNumberFormat = getNumberFormat;
    this.formatedNumberToStrNumber = formatedNumberToStrNumber;
    this.toFormatedNumber = toFormatedNumber;
    this.isUseableNumber = isUseableNumber;
    this.isUseableNumberNotEqualMinvalue = isUseableNumberNotEqualMinvalue;
    this.getNumber = getNumber;

    this.showResutl = showResutl;
    
    this.init(this.numberFormat);
    return this;
} // FormatNumberEntity()

function getNumber(formatedNumber) {
    return parseFloat(this.formatedNumberToStrNumber(formatedNumber));
} // getNumber()

function isUseableNumber(currentFormatedNumber, smallestNumber, bigestNumber) {
    var number = this.getNumber(currentFormatedNumber);
    if ((number >= smallestNumber) && (number <= bigestNumber)) {
        return true;
    } else {
        return false;
    } // else
} // isUseableNumber

function isUseableNumberNotEqualMinvalue(currentFormatedNumber, smallestNumber, bigestNumber) {
    var number = this.getNumber(currentFormatedNumber);
    if ((number > smallestNumber) && (number <= bigestNumber)) {
        return true;
    } else {
        return false;
    } // else
} // isUseableNumber

function showResutl () {
    var str = "";
    str += "numberFormat      = '" + this.numberFormat + "'" + "\n";
    str += "validNumberFormat = '" + this.validNumberFormat + "'" + "\n";
    str += "decimalSeparate   = '" + this.decimalSeparate + "'" + "\n";
    str += "fractionSeparate  = '" + this.fractionSeparate + "'" + "\n";
    str += "decimalGroupCount = '" + this.decimalGroupCount + "'" + "\n";
    str += "fractionCount     = '" + this.fractionCount + "'" + "\n";
    return(str);
} // showResutl(()

function initEntity(_formatString) {
    var validateFormatString = true;
    var formatStringLen = _formatString.length;
    var index = 0;
    var S1 = "";
    var S2 = "";
    var S3 = "";
    var S4 = "";
    var S5 = "";
    var S6 = "";

    if (formatStringLen > 0) {
        var charIndex = _formatString.substring(index, ++index);
        while (charIndex == "#" && index <= formatStringLen) {
            S1 += charIndex;
            charIndex = _formatString.substring(index, ++index);
        } // while

        while (charIndex != "#" && index <= formatStringLen) {
            S2 += charIndex;
            charIndex = _formatString.substring(index, ++index);
        } // while
        
        while (charIndex == "#" && index <= formatStringLen) {
            S3 += charIndex;
            charIndex = _formatString.substring(index, ++index);
        } // while

        while (charIndex != "#" && index <= formatStringLen) {
            S4 += charIndex;
            charIndex = _formatString.substring(index, ++index);
        } // while

        while (charIndex == "#" && index <= formatStringLen) {
            S5 += charIndex;
            charIndex = _formatString.substring(index, ++index);
        } // while

        while (index <= formatStringLen) {
            S6 += charIndex;
            charIndex = _formatString.substring(index, ++index);
        } // while

        if (S6.length == 0 && !containNumber(S2) && (!containNumber(S4) || S4 == "")) {
            this.validNumberFormat = true;
            this.decimalSeparate = S2;            // <> number
            this.decimalGroupCount = S3.length;   // <> 0
            if (S4 != "") {
                this.fractionSeparate = S4;           // <> number
                this.fractionCount = S5.length;       // == 0 OK
            } // if
        } else {
            this.validNumberFormat = false;
        } // else
    } // if
} // initEntity()

function containNumber(text) {
    if (text.length == 0) {
        return false;
    } else {
        var i = 0;
        var cont = true;
        while (cont && i < text.length) {
            var textTemp = text.substring(i, ++i);
            if (textTemp != " ") {
                cont = textTemp != "+" && textTemp != "-" && isNaN(textTemp); 
            } // if
        } // while
        return !cont;
    } // else
} // containNumber()

function getNumberFormat() {
    return this.numberFormat;
} // getNumberFormat

function isValidNumber (number) {
    var result = true;
    var newNumber = trim(number);
    if (newNumber.length == 0) { 
        result = false; 
    } else {
        newNumber = this.formatedNumberToStrNumber(newNumber);
        if (isNaN(newNumber)) {
            result = false;
        } else {
            result = true;
        } // else 
    } // else if
    return result
} // isValidNumber()

function formatedNumberToStrNumber(number) {
    var newNumber = trim(number);
    while (newNumber.indexOf(this.decimalSeparate) != -1) { 
        newNumber = newNumber.replace(this.decimalSeparate, "");
    } // while

    if (this.fractionSeparate != "") {
        newNumber = newNumber.replace(this.fractionSeparate, ".");
    } // if
    return newNumber;
} // formatedNumberToStrNumber()

function getBaseNumber(count) {
    var baseNumber = "1";
    if (count > 0) {
        for (var i = 0; i<count; i++) {
            baseNumber = baseNumber + "0";
        } // for
    } // if
    return baseNumber;
} // getBaseNumber

function toFormatedNumber(number) {
    var textNumber = this.formatedNumberToStrNumber(number);
    var textNumberArr = textNumber.split(".");
    var num1;
    var num2;

    if (textNumberArr == null) {
        num1 = textNumber;
        num2 = "0";
    } else {
        if (textNumberArr.length == 1) {
            num1 = textNumberArr[0];
            num2 = "0";
        } else {
            num1 = textNumberArr[0];
            num2 = textNumberArr[1];
        } // else
    } // else

    var sign = "";
    if (num1.substring(0,1) == "-") {
        sign = "-";
        num1 = num1.substring(1,num1.length);
    } else if (num1.substring(0,1) == "+") { 
        num1 = num1.substring(1,num1.length);
    } // else

    num2 = "0." + num2;
    var baseNumber = getBaseNumber(this.fractionCount); 
    var num3 = Math.round(eval(num2 + " * " + baseNumber)) / baseNumber;
    //var num4  = eval(num1 + " + " + num3);
    if(num3.toString().indexOf('0.') != -1) {
        var num4  = num1.toString() + num3.toString().substring(1);
    }
    else {
        var num4  = eval(num1 + " + " + num3);
    }
    //Strange bug in javascript (1 + 0.82 = 1.8199999999999998 ????) [
    if(num4 == 1.8199999999999998) {
        num4 = 1.82;
    }
    //]
    var num5 = ("" + num4).split('.')[0];
    var num6 = ("" + num4).split('.')[1];
    if(!num6) num6 = "";
    var num7 = "";
    if (num6 == "") {
        if (this.fullFormat) {
            num7 = getFormat(sign, num5, this.decimalSeparate, this.decimalGroupCount) + this.fractionSeparate + getPaddingZero("", this.fractionCount);
        } else {
            num7 = getFormat(sign, num5, this.decimalSeparate, this.decimalGroupCount);
        } // else
    } else {
        if (this.fullFormat) {
            num7 = getFormat(sign, num5, this.decimalSeparate, this.decimalGroupCount) + this.fractionSeparate + getPaddingZero(num6, this.fractionCount);
        } else {
            num7 = getFormat(sign, num5, this.decimalSeparate, this.decimalGroupCount) + this.fractionSeparate + num6;
        } // else
    }
    return num7;
} // toFormatedNumber()

function getPaddingZero(strNumber, len) {
    var result = strNumber;
    if (len > 0 && len > strNumber.length) {
        for (var i = strNumber.length; i < len; i++) {
            result = result + "0";
        } // for
    } // if
    return result;
} // getPaddingZero

function getFormat(sign, num, decimalSeparate, decimalGroupCount) {
    var result = "";
    if (decimalGroupCount > 0) {
        var resultTemp = "";
        var end = num.length;
        var start = end - decimalGroupCount;
        while (start >= 0) {
            if (resultTemp == "") {
                resultTemp = num.substring(start, end);
            }  else {
                resultTemp = num.substring(start, end) + decimalSeparate + resultTemp;
            } // else
            end = start;
            start = end - decimalGroupCount;
        } // while
        if (start < 0 && end > 0) {
            if (resultTemp == "") {
                resultTemp = num.substring(0, end);
            }  else {
                resultTemp = num.substring(0, end) + decimalSeparate + resultTemp;
            } // else
        } // if
        result = sign + resultTemp;
    } else {
        result = sign + num;
    } // else
    return result;
} // getFormat()


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

